#ifndef COLLECTION_H
#define COLLECTION_H
#include "worker.h"
#include <QMetaEnum>
#include <QObject>
#include <qthread.h>
#include "global.h"
#include "trigger.h"

class Collection: public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString speed READ speed NOTIFY signalDataChanged)
    Q_PROPERTY(QString temperature READ temperature NOTIFY signalDataChanged)
    Q_PROPERTY(QString weight READ weight NOTIFY signalDataChanged)
    Q_PROPERTY(QString ph READ ph NOTIFY signalDataChanged)

    Q_PROPERTY(int speedColor READ speedColor NOTIFY signalDataChanged)
    Q_PROPERTY(int temperatureColor READ temperatureColor NOTIFY signalDataChanged)
    Q_PROPERTY(int weightColor READ weightColor NOTIFY signalDataChanged)
    Q_PROPERTY(int phColor READ phColor NOTIFY signalDataChanged)
    Q_PROPERTY(int conductivityColor READ conductivityColor NOTIFY signalDataChanged)

    Q_PROPERTY(QString phTemp READ phTemp NOTIFY signalDataChanged)
    Q_PROPERTY(QString phUsageTime READ phUsageTime NOTIFY signalDataChanged)
    Q_PROPERTY(QString conductivity READ conductivity NOTIFY signalDataChanged)
    Q_PROPERTY(QString conTemp READ conTemp NOTIFY signalDataChanged)
    Q_PROPERTY(QString conUsageTime READ conUsageTime NOTIFY signalDataChanged)

    Q_PROPERTY(QString info READ info NOTIFY signalInfoChanged)
    Q_PROPERTY(Global::Result result READ result NOTIFY signalInfoChanged)
    Q_PROPERTY(Global::Cmd cmd READ cmd NOTIFY signalInfoChanged)
public:
    Collection(QObject *parent = nullptr);
    void start();

    Q_INVOKABLE bool calibrationWeight(int weight);
    Q_INVOKABLE bool calibration0();
    Q_INVOKABLE bool setCollect(bool run);
    Q_INVOKABLE bool toZero();
    Q_INVOKABLE bool toTare();
    Q_INVOKABLE bool setSpeed(int speed);
    Q_INVOKABLE bool phCalibration1();
    Q_INVOKABLE bool phCalibration2();
    Q_INVOKABLE bool conductivityCalibration1();
    Q_INVOKABLE bool conductivityCalibration2();
    Q_INVOKABLE bool setBuzzing(bool bBuzzing);
    Q_INVOKABLE bool showInfo(const QString& info);

signals:
    void signalDataChanged();
    void signalInfoChanged();
    void signalUpdateParam(const QString& type, const QString& value);
    void signalDoJob();

public slots:
    void slotUpdate(float speed, float temperature, float weight, float ph, float phTemp, float phUsageTime,
                    float conductivity, float conTemp, float conUsageTime);
    void slotInfo(Global::Cmd cmd, Global::Result result, QString info);
    void slotUpdateParam(const QString& param, const QString& value);

public:
    int     speedColor();
    int     temperatureColor();
    int     weightColor();
    int     phColor();
    int     conductivityColor();

    QString temperature();
    QString weight();
    QString ph();
    QString phTemp();
    QString phUsageTime();
    QString speed();
    QString conductivity();
    QString conTemp();
    QString conUsageTime();
    QString info();
    Global::Result result();
    Global::Cmd cmd();

private:
    void updateCollection();
    void checkAlarm(const QString& type, float value);

private:
    float   m_temperature;
    int     m_temperatureColor;
    float   m_weight;
    int     m_weightColor;
    float   m_PH;
    int     m_PHColor;
    float   m_phTemp;
    float   m_phUsageTime;
    float   m_speed;
    int     m_speedColor;
    float   m_conductivity;
    int     m_conductivityColor;
    float   m_conductivityTemp;
    float   m_conductivityUsageTime;

    int   m_count;              //计数器，每10秒记录一次日志
    int   m_periodOfRecord;     // 数据记录周期

    QString m_info;
    Global::Result  m_result;
    Global::Cmd     m_cmd;
    Worker*         m_pWorker;
    Trigger*        m_pTrigger;
};

#endif // COLLECTION_H
